package de.tabuma.sso.privileg;

import de.tabuma.sso.logik.*;
import de.tabuma.sso.project.*;
import de.tabuma.sso.groupprivileg.*;

import java.util.*;
import java.util.logging.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.2.1 $
 * @since $Date: 2010/01/08 10:03:24 $
 */
public class PrivilegFacade implements de.tabuma.lib.contentmanagement.FacadeInterface {

    private static final Logger log = Logger.getLogger(PrivilegFacade.class.getName());

    public static int create(Privileg privileg, String user) {
        return (int) PMF.doCreate(privileg, user);
    }

    public int createElement(Object object, String user) {
        return create((Privileg)object,user);
    }
    
    public static Privileg get(int id) {
        return (Privileg) PMF.get(id, new Privileg());
    }

    public Object getElement(int id) {
        return get(id);
    }

    public static Privileg get(int projectId, String privilegName) {
        String key[] = new String[2];
        key[0] = "name";
        key[1] = "projectId";
        String value[] = new String[2];
        value[0] = privilegName;
        value[1] = "" + projectId;
        List<Privileg> pList = PMF.get(key, value, new Privileg());
        if(pList.size()>0) {
            return pList.get(0);
        }
        return null;
    }

    public static void update(Privileg privileg, String user) {
        PMF.doUpdate(privileg, user);
    }

    public void updateElement(Object object, String user) {
        update((Privileg)object, user);
    }

    public static void remove(int id, String user) {
        Privileg privileg = get(id);
        PMF.doRemove(privileg, user);
        List<GroupPrivileg> gpList = GroupPrivilegFacade.getList1(id);
        for(int i=0;i<gpList.size();i++) {
            GroupPrivileg gp = gpList.get(i);
            GroupPrivilegFacade.remove(gp.getId().intValue(), user);
        }
    }

    public void removeElement(int id, String user) {
        remove(id, user);
    }

    public static List<Privileg> getList(int projectId) {
        String keys[] = new String[1];
        keys[0] = "projectId";
        String values[] = new String[1];
        values[0] = "" + projectId;
        return PMF.get(keys, values, new Privileg());
    }

    public static List<Privileg> getList(String user) {
        List<Project> pList = ProjectFacade.getList(user);
        List<Privileg> prList = new ArrayList<Privileg>();
        for(int i=0;i<pList.size();i++) {
            prList.addAll(getList(pList.get(i).getId().intValue()));
        }
        return prList;
    }

    public List getElementList(String listParameter) {
        Integer value = Integer.valueOf(listParameter).intValue();
        return getList(value);
    }


}
